package com.fy.fyspace.controller;

import com.fy.fyspace.common.annotation.GlobalInterceptor;
import com.fy.fyspace.common.result.Result;
import com.fy.fyspace.constant.KeysConstant;
import com.fy.fyspace.model.entity.Space;
import com.fy.fyspace.model.enums.space.SpaceLevelEnum;
import com.fy.fyspace.model.vo.space.SpaceVO;
import com.fy.fyspace.model.vo.user.UserSessionVO;
import com.fy.fyspace.service.SpaceService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import java.util.List;

import static cn.hutool.core.bean.BeanUtil.copyProperties;

/**
 * 空间管理控制器
 */
@Api(tags = "个人空间模块")
@Slf4j
@RestController
@RequestMapping("/space")
public class SpaceController {

    @Autowired
    private SpaceService spaceService;

    /**
     * 获取个人空间详情
     * 
     * @param httpSession
     * @return
     */
    @ApiOperation(value = "获取个人空间详情")
    @GlobalInterceptor
    @GetMapping("/getSpace")
    public Result getSpace(HttpSession httpSession) {
        UserSessionVO userSessionVO = (UserSessionVO) httpSession.getAttribute(KeysConstant.LOGIN_SESSION_KEY);
        Space space = spaceService.getById(userSessionVO.getId());
        SpaceVO spaceVO = new SpaceVO();
        copyProperties(space, spaceVO);
        spaceVO.setSpaceLevel(SpaceLevelEnum.getTextByValue(Math.toIntExact(space.getSpaceLevel())));
        return Result.success(spaceVO);
    }

    /**
     * 获取当前用户不同审核状态的图片列表
     * 
     * @param pageNo      页码
     * @param pageSize    每页大小
     * @param httpSession 会话信息
     * @return 分页结果
     */
    @ApiOperation(value = "获取当前用户待审核的图片列表")
    @GlobalInterceptor
    @GetMapping("/getReviewPictures")
    public Result getReviewingPictures(
            @ApiParam(value = "页码", required = true) @RequestParam(defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页大小", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "每页大小", required = true) @RequestParam Integer reviewStatus,
            HttpSession httpSession) {
        log.info("开始获取当前用户待审核图片列表: pageNo={}, pageSize={}, reviewStatus={}", pageNo, pageSize, reviewStatus);
        // 获取当前登录用户
        UserSessionVO userSessionVO = (UserSessionVO) httpSession.getAttribute(KeysConstant.LOGIN_SESSION_KEY);
        return spaceService.getReviewPictures(pageNo, pageSize, reviewStatus, userSessionVO.getId());
    }

    /**
     * 批量删除被反驳照片
     *
     * @param pictureIds 图片ID数组
     */
    @ApiOperation(value = "批量删除被反驳照片")
    @GlobalInterceptor
    @PostMapping("/batchDeleteRejectPictures")
    public Result batchDeleteRejectPictures(@RequestBody List<Long> pictureIds) {
        log.info("批量删除被反驳照片: pictureIds={}", pictureIds);
        return spaceService.batchDeleteRejectPictures(pictureIds);
    }

    /**
     * 获取AI图列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取AI图列表", notes = "根据查询条件获取AI图列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 400, message = "服务器内部错误")
    })
    @GetMapping("/getAiPictureList")
    public Result getAiPictureList(
            @ApiParam(value = "页码", example = "1") @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "每页数量", example = "30") @RequestParam(required = false) Integer pageSize,
            @ApiParam(value = "搜索关键词", example = "风景") @RequestParam(required = false) String searchText,
            HttpSession httpSession) {
        log.info("查询AI图列表, 参数: pageNo={}, pageSize={}, searchText={}", pageNo, pageSize, searchText);
        UserSessionVO loginUser = (UserSessionVO) httpSession.getAttribute(KeysConstant.LOGIN_SESSION_KEY);
        return Result.success(spaceService.getAiPictureList(pageNo, pageSize, searchText, loginUser.getId()));
    }
}
